package staff;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;

public class add extends JFrame {
    private static final long serialVersionUID = -1928970409928880648L;
    JLabel jlpno = new JLabel("工      号：");
    JLabel jlname = new JLabel("姓      名：");
    JLabel jlsex = new JLabel("性      别：");
    JLabel jlbirth = new JLabel("出 生 年 月：");
    JLabel jleducation = new JLabel("年      龄：");
    JLabel jlpolitics = new JLabel("政 治 面 貌：");
    JLabel jlmarriage = new JLabel( "婚 姻 状 况：");
    JLabel jlphone = new JLabel( "联 系 电 话：");
    JLabel jlsection = new JLabel( "部      门：");
    JLabel jlwork = new JLabel("职      务：");

    JTextField jtpno = new JTextField("",20);
    JTextField jtname = new JTextField("",20);
    JTextField jtsex = new JTextField("",20);
    JTextField jtbirth = new JTextField("",20);
    JTextField jteducation = new JTextField("",20);
    JTextField jtpolitics = new JTextField("",20);
    JTextField jtmarriage = new JTextField("",20);
    JTextField jtphone = new JTextField("",20);
    JTextField jtsection = new JTextField("",20);
    JTextField jtwork = new JTextField("",20);

    JButton buttonadd = new JButton("添加");
    JButton buttonreturn = new JButton("返回");

    public add() {
        JPanel jppno = new JPanel();
        JPanel jpname = new JPanel();
        JPanel jpsex = new JPanel();
        JPanel jpbirth = new JPanel();
        JPanel jpeducation = new JPanel();
        JPanel jppolitics = new JPanel();
        JPanel jpmarriage = new JPanel();
        JPanel jpphone = new JPanel();
        JPanel jpsection = new JPanel();
        JPanel jpwork = new JPanel();

        JPanel jpforbutton = new JPanel(new GridLayout(1,1));

        jppno.add(jlpno);
        jppno.add(jtpno);

        jpname.add(jlname);
        jpname.add(jtname);

        jpsex.add(jlsex);
        jpsex.add(jtsex);

        jpbirth.add(jlbirth);
        jpbirth.add(jtbirth);

        jpeducation.add(jleducation);
        jpeducation.add(jteducation);

        jppolitics.add(jlpolitics);
        jppolitics.add(jtpolitics);

        jpmarriage.add(jlmarriage);
        jpmarriage.add(jtmarriage);

        jpphone.add(jlphone);
        jpphone.add(jtphone);

        jpsection.add(jlsection);
        jpsection.add(jtsection);

        jpwork.add(jlwork);
        jpwork.add(jtwork);

        jpforbutton.add(buttonadd);
        jpforbutton.add(buttonreturn);

        buttonadd.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){

                boolean flag =false;
                flag= !jtname.getText().equals("") && !jtwork.getText().equals("");

                System.out.println(flag);
                if ( !flag) {

                    if(e.getSource()==buttonadd){
                        javax.swing.JOptionPane.showMessageDialog(null, "添加格式有误！！！重新添加！添加操作结束!");
                    }

                } else {

                    Connection Test = null;
                    PreparedStatement ps = null;
                    PreparedStatement ps1 = null;
                    PreparedStatement ps2 = null;
                    PreparedStatement ps3 = null;
                    PreparedStatement ps4 = null;
                    PreparedStatement ps5 = null;
                    PreparedStatement ps6 = null;

                    String xpno = jtpno.getText();
                    String xname = jtname.getText();
                    String xsex = jtsex.getText();
                    String xbirth = jtbirth.getText();
                    String xeducation = jteducation.getText();
                    String xpolitics = jtpolitics.getText();
                    String xmarriage = jtmarriage.getText();
                    String xphone = jtphone.getText();
                    String xsection = jtsection.getText();
                    String xwork = jtwork.getText();


                    String sql = "INSERT INTO allstaff(pno,name,xingbie,birth,age,politics,marriage,phone,section,zhiwu) "
                            + "values(?,?,?,?,?,?,?,?,?,?)";
                    String sql2 = "INSERT INTO xx(pno)" + "values(?)";
                    String sql3 = "INSERT INTO cz(pno)" + "values(?)";
                    String sql4 = "INSERT INTO gz(pno)" + "values(?)";
                    String sql5 = "INSERT INTO dx(pno)" + "values(?)";
                    String sql6 = "INSERT INTO ss(pno)" + "values(?)";
                    String sql7 = "INSERT INTO bs(pno)" + "values(?)";

                    try {
                        Class.forName("com.mysql.cj.jdbc.Driver");
                        System.out.println("JBDC 加载成功!");
                    } catch (Exception a) {
                        System.out.println("JBDC 狗带!");
                        a.printStackTrace();
                    }
                    try {
                        Test = DriverManager.getConnection("jdbc:mysql://localhost:3306/work", "root", "123456");

                        ps = Test.prepareStatement(sql);
                        ps1 = Test.prepareStatement(sql2);
                        ps2 = Test.prepareStatement(sql3);
                        ps3 = Test.prepareStatement(sql4);
                        ps4 = Test.prepareStatement(sql5);
                        ps5 = Test.prepareStatement(sql6);
                        ps6 = Test.prepareStatement(sql7);
                        ps1.setString(1, xpno);
                        ps2.setString(1, xpno);
                        ps3.setString(1, xpno);
                        ps4.setString(1, xpno);
                        ps5.setString(1, xpno);
                        ps6.setString(1, xpno);

                        ps.setString(1, xpno);
                        ps.setString(2, xname);
                        ps.setString(3, xsex);
                        ps.setString(4, xbirth);
                        ps.setString(5, xeducation);
                        ps.setString(6, xpolitics);
                        ps.setString(7, xmarriage);
                        ps.setString(8, xphone);
                        ps.setString(9, xsection);
                        ps.setString(10, xwork);

                        ps.executeUpdate();
                        ps1.executeUpdate();
                        ps2.executeUpdate();
                        ps3.executeUpdate();
                        ps4.executeUpdate();
                        ps5.executeUpdate();
                        ps6.executeUpdate();
                        if (e.getSource() == buttonadd) {
                            javax.swing.JOptionPane.showMessageDialog(null, "添加成功!");
                        }

                    } catch (SQLException b) {
                        System.out.println("添加失败！请重新添加");
                        if (e.getSource() == buttonadd) {
                            javax.swing.JOptionPane.showMessageDialog(null, "添加失败！！！重新添加！添加操作结束!");
                        }
                    } finally {

                        try {
                            Test.close();
                            System.out.println("MySQL 关闭成功");
                        } catch (SQLException c) {
                            System.out.println("MySQL 关闭失败 ");
                            c.printStackTrace();
                        }
                    }
                }
            }
        });
        buttonreturn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {dispose();
            }
        });
        this.setTitle("添加教师信息");
        this.setLayout(new GridLayout(11, 1));
        this.add(jppno);
        this.add(jpname);
        this.add(jpsex);
        this.add(jpbirth);
        this.add(jpeducation);
        this.add(jppolitics);
        this.add(jpmarriage);
        this.add(jpphone);
        this.add(jpsection);
        this.add(jpwork);
        this.add(jpforbutton);
        this.setLocation(400, 300);
        this.setSize(350, 300);
        this.setVisible(true);
    }

}

